<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- el-card组件 -->
      <el-card class="box-card">
        <!-- el-tab组件 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- layout 布局组件 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                        <!-- <el-dropdown-item @click.native="editDept('')">编辑部门</el-dropdown-item>
                        <el-dropdown-item>删除部门</el-dropdown-item> -->
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 树形控件渲染结构 -->
            <el-tree :data="treeData" :default-expand-all="true" class="departments-tree">
              <template v-slot="{ data }">
                <el-row type="flex" justify="space-between" align="middle" style="width:100%">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          v-if="showDialog"
          :title="isEdit ? '编辑组织架构' : '添加组织架构'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <dept-dialog :parent-id="parentId" :is-edit="isEdit" :origin-list="originList" @update-list="updateList" @close="closeConfirm" />

        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog'
export default {
  components: { deptDialog },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      // 定义结构显示 配合:props="defaultProps"
      // defaultProps: {
      //   children: 'children',
      //   lable: 'name'
      // }
      showDialog: false, // 控制添加或编辑弹框展示
      parentId: '',
      isEdit: false, // 是否是编辑
      originList: [] // 精简后的数据
    }
  },
  created() {
    // 调用获取组织架构数据的方法
    this.getDepartments()
  },
  methods: {
    // 删除子部门
    async delDept(id) {
      // 显示删除确认消息对话框
      const comfirmRes = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(comfirmRes)
      if (comfirmRes === 'cancel') return this.$message('您已取消删除')
      // 点击确定 调用API
      const res = await delDepartments(id)
      this.$message.success(res.message)
      this.getDepartments()
    },
    // 点击编辑
    editDept(id) {
      this.parentId = id
      this.showDialog = true
      this.isEdit = true // 点击编辑时把isEdit设置为true
    },
    // 隐藏新增/编辑弹框
    closeConfirm() {
      this.showDialog = false
    },
    // 更新列表
    updateList() {
      this.showDialog = false
      this.getDepartments()
    },
    // 显示 添加子部门弹框
    addDept(id) {
      // console.log(id)
      this.parentId = id
      this.showDialog = true
      this.isEdit = false // 点击添加时把isEdit设置为false
    },
    // 组织架构数据的获取方法
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 根据后端返回的状态码进行错误提示
      if (!res.success) return this.$message.error(res.message)
      // 格式化需要传递给子组件的数据
      this.originList = res.data.depts.map(item => {
        return {
          name: item.name,
          id: item.id,
          code: item.code,
          pid: item.pid
        }
      })
      // 将获取到的数据进行赋值
      this.treeData = tranListToTreeData(res.data.depts)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>

